package cn.coopen.italktv.demo02_otherview;

import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;

import java.util.ArrayList;
import java.util.List;

import cn.coopen.italktv.demo02_otherview.fragment.PopFragment;

public class MainActivity extends AppCompatActivity {

	TabLayout tabLayout;
	ViewPager pager;
	List<PageModel> pageModels = new ArrayList<>();

	{
		pageModels.add(new PageModel(R.string.model_demo, PopFragment.newInstance()));

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public Fragment getItem(int position) {
				PageModel pageModel = pageModels.get(position);
				return pageModel.child;
			}

			@Override
			public int getCount() {
				return pageModels.size();
			}

			@Override
			public CharSequence getPageTitle(int position) {
				return getString(pageModels.get(position).titleRes);
			}
		});

		tabLayout = (TabLayout) findViewById(R.id.tabLayout);
		tabLayout.setupWithViewPager(pager);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	private class PageModel {
		Fragment child;

		@StringRes
		int titleRes;

		PageModel(@StringRes int titleRes, Fragment child) {
			this.child = child;
			this.titleRes = titleRes;
		}
	}
}
